<?php
/*
  Template Name: Fechar Pedido
 */
?>
<?
get_header();
?>

<td class="td_carrinho">
	<div id="carrinho">
		<div class="area_carrinho">
			<?
			$user_cep = $_POST['sentCep'];
			$cep = str_replace("-", "", $user_cep);
			$json = simple_curl("http://cep.correiocontrol.com.br/{$cep}.json");
			$data = json_decode($json);
			if (count($_SESSION['produtos'])) {

				foreach ($_SESSION['produtos'] as $id => $arr) {
					$post = get_post($id);

					if (get_post_meta($post->ID, 'meta_box_frete', true) != 'on') { // on = gratis
						if (get_post_meta($post->ID, 'meta_box_frete_valor', true) != '0,00') { // valor fixo
							$valor_frete = get_post_meta($post->ID, 'meta_box_frete_valor', true) * $arr['qtd'];
							$prazo_fixo = get_post_meta($post->ID, 'meta_box_prazo', true);
						} else if ($user_cep) { // calculo dinâmico pelo cep
							$comprimento = get_post_meta($post->ID, 'meta_box_comprimento', true) * $arr['qtd'];
							$altura = get_post_meta($post->ID, 'meta_box_altura', true) * $arr['qtd'];
							$largura = get_post_meta($post->ID, 'meta_box_largura', true) * $arr['qtd'];
							$peso = get_post_meta($post->ID, 'meta_box_peso', true) * $arr['qtd'];
							$frete = get_frete(22755158, $user_cep, $peso, $comprimento, $altura, $largura);
							$valor_frete = $frete['preco'];
							$prazo_fixo = $frete['prazo'];
						} else {
							$valor_frete = '0,00';
							$prazo_fixo = '0';
						}
					} else { // frete gratis
						$valor_frete = '0,00';
						$prazo_fixo = get_post_meta($post->ID, 'meta_box_prazo', true);
					}
					$prazo = ((int) $prazo_fixo > (int) $prazo) ? $prazo_fixo : $prazo; // linha do prazo
					$total_frete = str_replace(",", ".", $valor_frete);
					$base_frete += str_replace(",", ".", $total_frete);
				}


				$referencia = rand(1000000, 9999999);

				foreach ($_SESSION['produtos'] as $id => $arr) {

					//unset($valor_frete, $total_frete);

					$post = get_post($id);
					$meta = get_post_custom($id);

					$items[$id]['id'] = $post->ID;
					$items[$id]['nome'] = $post->post_title;
					$items[$id]['valor'] = (str_replace(",", ".", $arr['preco'])) * 100;
					$items[$id]['qtd'] = $arr['qtd'];

					$valor_total = ($items[$id]['valor'] * $items[$id]['qtd']) + $valor_total;
				}

				$valor_total = $valor_total + ($base_frete * 100);

				$pedido['referencia'] = $referencia;
				$pedido['frete'] = $base_frete * 100;
			}
			get_pagseguro($pedido, $items);
			?>

			<div class="pedido">
				<div class="referencia">
					PEDIDO NUMERO: <?= $referencia; ?>
				</div>


				<div id="cadastroDados"> 
					<div class="identificacao">Resumo do pedido</div>
					<br />
					<br />
					<div class="campo">
						<p><b>Produtos</b></p>

						<?
						foreach ($_SESSION['produtos'] as $id => $arr) {
							$post = get_post($id);
							if (has_post_thumbnail($post->ID)) {
								$default = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large');
								$img = $default[0];
							} else {

								$args = array(
								    'post_type' => 'attachment',
								    'numberposts' => -1,
								    'post_status' => null,
								    'post_parent' => $post->ID
								);

								$attachments = get_posts($args);
								if ($attachments) {
									foreach ($attachments as $attachment) {
										$default = wp_get_attachment_image_src($attachment->ID, 'large');
										break;
									}
									$img = $default[0];
								}
							}

							/* ######################################################################## */
							/* ######################################################################## */
							/* ######################################################################## */

							$base_preco = str_replace(",", ".", $arr['preco']);
							$base_preco = $base_preco * $arr['qtd'];
							$valor_total_pedido = $valor_total_pedido + $base_preco;
							$base_preco = number_format($base_preco, 2, ',', '.');
							$topline = 'topline';
							?>

							<?
							echo "- " . get_the_title() . "<br />";
						}

						echo "<br />Subtotal: R$ " . number_format($valor_total_pedido, 2, ',', '.') . "<br />";
						?>

					</div>					
					<br />
					<br />

					<div class="identificacao">Endere&ccedil;o para entrega</div>
					<br />
					<br />
					<div class="painel">
						<div class="campo">
							<p><b>CEP</b></p>
							<input id="cepz" name="cepz" value="<?= $user_cep; ?>" tabindex="1" class="verify inbox1" type="text" disabled>
							<input id="cep" name="cep" value="<?= $user_cep; ?>" type="hidden">
						</div>
						<div class="campo">
							<p><b>Endereco</b></p>
							<input id="endereco" placeholder="Endereco" name="endereco" value="<?= $data->logradouro; ?>" tabindex="1" class="inbox5" type="text">
						</div>
						<div class="campo">
							<p><b>Numero</b></p>
							<input id="numero" placeholder="Numero" name="numero" value="" tabindex="1" class="inbox1 verify" type="text">
						</div>
						<div class="campo">
							<p><b>Complemento</b></p>
							<input id="complemento" placeholder="Complemento" name="complemento" value="" tabindex="1" class="inbox3" type="text">
						</div>
						<div class="campo">
							<p><b>Bairro</b></p>
							<input id="bairro" placeholder="Bairro" name="bairro" value="<?= $data->bairro; ?>" tabindex="1" class="inbox3" type="text">
						</div>
						<div class="campo">
							<p><b>Cidade</b></p>
							<input id="cidade" placeholder="Cidade" name="cidade" value="<?= $data->localidade; ?>" tabindex="1" class="inbox3" type="text">
						</div>
						<div class="campo">
							<p><b>Estado</b></p>
							<input id="estado" placeholder="Estado" name="estado" value="<?= $data->uf; ?>" tabindex="1" class="inbox1" type="text">
						</div>
					</div>
					<br />
					<input id="validaForm" type="image" src="<?= get_bloginfo('template_url'); ?>/img/botao_pagseguro.gif" alt="Imprimir Boleto" />
					<input type="hidden" name="valor_total" value="<?= number_format($valor_total / 100, 2, ',', '.'); ?>" /><br />
				</form>
				</div>
			</div>
		</div>
	</div>
</td>
<? get_footer(); ?>